package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.GoodsDetailBean;
import com.sanyang.pda.bean.request.SamplingSubmitRequest;
import com.sanyang.pda.contract.SamplingContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class SamplingPresenter extends BasePresenter<SamplingContract.View> implements SamplingContract.Presenter {

    @Override
    public void samplingArticleInfoSubmit(SamplingSubmitRequest samplingSubmitRequest) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(samplingSubmitRequest));
        NetUtils.geApi().samplingArticleInfoSubmit(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<GoodsDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<GoodsDetailBean> goodsDetailBeanBaseAppEntity) throws Exception {
                        getView().submitSuccess();
                        getView().showMessage("取样成功");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void selectArticleInfoByBarcode(String barcode,String number) {
        NetUtils.geApi().selectArticleInfoByBarCodeAndNo(barcode,number)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ByBarcodeBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ByBarcodeBean>> listBaseAppEntity) throws Exception {
                        getView().setGoodsList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
